package proxy_pattern.remote.ex2;

import proxy_pattern.remote.ex2.to.Request;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 * 该类是客户端入口，负责与远程服务进行交互。
 * @author 申书航
 * @version 1.0
 */
public class Client {

    /**
     * 主方法，程序的入口点。用于初始化客户端并进行编码和解码请求。
     *
     * @param args  命令行参数（未使用）
     */
    public static void main(String[] args) {
        String host = "localhost";
        int port = 3000;
        //查找到注册的naming节点
        Registry registry = null;
        try {
            registry = LocateRegistry.getRegistry(host, port);
            IBase64 stub = (IBase64) registry.lookup("base64");
            //构建请求数据
            Request request = new Request();
            request.setId(1);
            request.setMessage("你好哇！");
            //调用加密
            String response = stub.encode(request);
            System.out.println("response: " + response);
            //调用解密
            Request decode = stub.decode(response);
            System.out.println(decode);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        } catch (NotBoundException e) {
            throw new RuntimeException(e);
        }
    }
}
